package com.hxd.otc.module.login.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.module.login.bean.PhoneCodeBean;
import com.hxd.otc.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/5/18 15:52
 * 描  述 :  ${电话区号Adapter}
 */
public class PhoneCodeAdapter extends RecyclerView.Adapter<PhoneCodeAdapter.ViewHolder> {
    private Context mContext;
    private List<PhoneCodeBean> mData = new ArrayList<>();
    boolean isAddBank;

    public void setmData(List<PhoneCodeBean> data) {
        if (mData != null)
            mData.clear();
        this.mData.addAll(data);
        LogUtils.e("====data=======" + data);
        LogUtils.e("====mData=======" + mData);
        notifyDataSetChanged();
    }

    public void setAddBank(boolean addBank) {
        isAddBank = addBank;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    private OnItemClickListener onItemClickListener;


    public PhoneCodeAdapter(@NonNull Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_phone_code_view, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        LogUtils.e("======onBindViewHolder=======" + mData.get(position).getCode());
        holder.mCodeText.setText("+" + mData.get(position).getCode());
        holder.mNameText.setText(mData.get(position).getChineseName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.itemClick(mData.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {


        private TextView mNameText;
        private TextView mCodeText;

        public ViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View itemView) {
            mNameText = itemView.findViewById(R.id.name_tv);
            mCodeText = itemView.findViewById(R.id.code_tv);
        }
    }

    public interface OnItemClickListener {
        void itemClick(PhoneCodeBean phoneCode);
    }

    public void setNewData(List<PhoneCodeBean> list) {
        mData = list;
        notifyDataSetChanged();
    }
}
